<?php
require_once 'AuthController.php';

class Advertisement extends CI_Controller {
	private $image;
	
	
	function __construct() {
		parent::__construct ();
		$this->load->model ( 'be/advertisement_model', 'model' );
		$this->load->helper ( 'form' );
		$this->load->helper ( 'url' );
		$this->load->library ( 'form_validation' );
	}
	
	function get_by_fillter(){
		$data ['list'] = $this->model->get_by_fillter ($this->input->post('status'),$this->input->post('type'));
		$data ['page_title'] = 'Danh sách quảng cáo';
		$data ['main_content'] = 'be/advertisement/view';
		$data ['system_pages_content'] = 'be/advertisement/list';
		$this->load->view ( 'be/includes/template', $data );
	}
	
	public function index() {
		$this->list_view ();
	}
	
	public function list_view() {
		$auth = new AuthController();
		if($auth->can('view', 'adv')) {
			$data ['list'] = $this->model->get_advertisement ();
			$data ['page_title'] = 'Danh sách quảng cáo';
			$data ['main_content'] = 'be/advertisement/view';
			$data ['system_pages_content'] = 'be/advertisement/list';
			$this->load->view ( 'be/includes/template', $data );
		}else {
			$auth->denied();
		}
	}
	
	public function add() {
		$auth = new AuthController();
		if($auth->can('add', 'adv')) {
			$this->form_validation->set_rules ( 'name', 'Tên quảng cáo', 'required' );
			if($this->input->post('type')=='banner'){
				$this->form_validation->set_rules ( 'image', 'Ảnh quảng cáo', 'required, callback_get_image_uploaded' );
				$this->form_validation->set_rules ('link','Link', 'required');
			}
			else
			{
				$this->form_validation->set_rules ('source','Source', 'required');
			}
			$this->form_validation->set_rules ( 'position', 'Vị trí' );
			$this->form_validation->set_rules ( 'description', 'Mô tả' );
			$this->form_validation->set_rules ( 'status', 'Trạng thái' );
			
			if ($this->form_validation->run () == FALSE) {
				$data ['page_title'] = 'Thêm mới quảng cáo';
				$data ['main_content'] = 'be/advertisement/view';
				$data ['system_pages_content'] = 'be/advertisement/add';
				$this->load->view ( 'be/includes/template', $data );
			} else {
				$data ['name'] = $this->input->post ( 'name' );
				$data ['position'] = $this->input->post ( 'position' );
				$data ['description'] = $this->input->post ( 'description' );
				$data ['type'] = $this->input->post ( 'type' );
	
				if($this->input->post ( 'type' ) == 'banner'){
					$data ['image']	 = $this->image;
					$data ['source'] = $this->input -> post('link');
				}
				else{
					$data ['source'] = $this->input -> post('source');
				}
				$data ['status'] = $this->input->post ( 'status' );
				
				if (! $this->model->add ( $data )) {
					$data ['page_title'] = 'Có lỗi xảy ra';
					$mess_detail = '<p>Hãy kiểm tra lại thông tin bạn đã điền vào form!</p>';
					$mess_detail .= '<p><a href="' . base_url () . 'index.php/advertisement/add"> Làm lại</a> hoặc <a href="' . base_url () . 'index.php/advertisement">Trở lại trang danh sách</a></p>';
					$data ['message_detail'] = $mess_detail;
				} else {
					$data ['page_title'] = 'Thành công';
					$mess_detail = '<p>Cập nhật thành công!</p>';
					$mess_detail .= '<p><a href="' . base_url () . 'index.php/advertisement/add"> Làm lại</a> hoặc <a href="' . base_url () . 'index.php/advertisement">Trở lại trang danh sách</a></p>';
					$data ['message_detail'] = $mess_detail;
				}
				$data ['main_content'] = 'be/customer/message';
				$this->load->view ( 'be/includes/template', $data );
			}
		}else {
			$auth->denied();
		}
	}
	
	function get_image_uploaded() {
		//Create location if it does not exist
		$location = DEFAULT_IMAGE_UPLOAD_PATH . 'banner';
		if (! is_dir ( $location )) {
			if (! mkdir ( $location, 0755 )) {
				die ( 'Failed to create folders...' );
			}
		}
		$config ['upload_path'] = $location;
		$config ['allowed_types'] = 'gif|jpg|png|bmp|jpeg';
		$config ['max_size'] = '10000';
		
		$this->load->library ( 'upload', $config );
		if ($this->upload->do_upload ( 'image' )) {
			$tmp = $this->upload->data ( 'image' );
			$this->image = $location . '/' . $tmp ['file_name'];
		} else {
			$this->image =  CUSTOMER_DEFAULT_AVATAR;
		}
	}
	
	public function update($id = 1) {
		$auth = new AuthController();
		if($auth->can('update', 'adv')) {
			$advertisement = $this->model->get_advertisement ( $id );
			
			$data ['id'] = $id;
			$data ['ad'] = $advertisement;
			
			$this->form_validation->set_rules ( 'name', 'Tên quảng cáo', 'required' );
			if($this->input->post('type') == 'banner'){
				$this->form_validation->set_rules ( 'image', 'Banner', 'callback_get_image_uploaded' );
			}
			$this->form_validation->set_rules ( 'source', 'Source','required' );
			$this->form_validation->set_rules ( 'position', 'Vị trí' );
			$this->form_validation->set_rules ( 'description', 'Mô tả' );
			
			$this->form_validation->set_rules ( 'status', 'Trạng thái' );
			
			if ($this->form_validation->run () == FALSE) {
				$data ['page_title'] = 'Cập nhật thông tin quảng cáo';
				$data ['main_content'] = 'be/advertisement/view';
				$data ['system_pages_content'] = 'be/advertisement/update';
				$this->load->view ( 'be/includes/template', $data );
			} else {
				$newdata ['id'] = $id;
				$newdata ['name'] = $this->input->post ( 'name' );
				$newdata ['position'] = $this->input->post ( 'position' );
				$newdata ['description'] = $this->input->post ( 'description' );
				$newdata ['status'] = $this->input->post ( 'status' );
				//echo $this->input->post('type');
	
				if($this->input->post('type')=='banner' ){
					if($this->image == CUSTOMER_DEFAULT_AVATAR) {
						//echo "vao if";
						$newdata['image'] = $advertisement['image'];
					}
					else {	
						$newdata['image'] = $this->image;	
					}
					//$data ['source'] = $this->input->post ( 'source' );
				}
				else {
					$newdata['source'] = $this->input->post ( 'source' );
				}		
				
				if (! $this->model->update ( $id, $newdata )) {
					$data ['page_title'] = 'Có lỗi xảy ra';
					$mess_detail = '<p>Hãy kiểm tra lại thông tin bạn đã điền vào form!</p>';
					$mess_detail .= '<p><a href="' . base_url () . 'index.php/advertisement/update/' . $id . '"> Làm lại</a> hoặc <a href="' . base_url () . 'index.php/advertisement">Trở lại trang danh sách</a></p>';
					$data ['message_detail'] = $mess_detail;
				} else {
					$data ['page_title'] = 'Thành công';
					$mess_detail = '<p>Cập nhật thành công!</p>';
					$mess_detail .= '<p><a href="' . base_url () . 'index.php/advertisement/update/' . $id . '"> Làm lại</a> hoặc <a href="' . base_url () . 'index.php/advertisement">Trở lại trang danh sách</a></p>';
					$data ['message_detail'] = $mess_detail;
				}
				$data ['main_content'] = 'be/customer/message';
				$this->load->view ( 'be/includes/template', $data );
			}
		}
		else {
			$auth->denied();
		}
	}
	
	public function delete($id) {
		$auth = new AuthController();
		if($auth->can('delete', 'adv')) {
			$ids = $this->input->post ( 'selected' );
			foreach ( $ids as $id ) {
				$this->model->delete ( $id );
			}
			redirect ( site_url ( 'advertisement' ) );
		}else {
			$auth->denied();
		}
	}
}